import { defineConfig, loadEnv } from 'vite'
import react from '@vitejs/plugin-react'
import { createHtmlPlugin } from 'vite-plugin-html'
import viteCompression from 'vite-plugin-compression'
import path from 'path'
// 声明一个函数用于获取当前的环境
const getViteEnv = (mode, target) => {
  return loadEnv(mode, process.cwd())[target]
}
export default defineConfig(({ command, mode, ssrBuild }) => {
  return {
    plugins: [react(), viteCompression(), createHtmlPlugin({
      inject: {
        data: {
          title: getViteEnv(mode, 'VITE_APP_TITLE')
        }
      }
    })],
    //项目部署在主域名的子文件使用,例如http://localhost:3000/myvite/。不填默认就是/
    base: '/',
    resolve: {
      alias: {
        '@': path.resolve(__dirname, './src'),
        '@img': path.resolve(__dirname, './src/assets/image')
      }
    },
    server: {
      host: '0.0.0.0',
      port: 996,
      open: true,
      hmr: true,
      proxy: {
        // '/api': {
        //   target: ' http://gateway.mzqswkj.com',
        //   changeOrigin: true,
        //   rewrite: (path) => path.replace(/^\/api/, ''),
        // }
      }
    },
    optimizeDeps: {
      esbuildOptions: {
        define: {
          global: 'globalThis'
        }
      }
    }
  }
})

